<!DOCTYPE html>
<html xmlns:layout="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
	layout:decorate="~{common/layout}">

<head>
	<meta charset="UTF-8">
	<title>血糖记录管理系统</title>
	<style scoped lang="less">
	.overlay-wrapper {
	    display: flex;
	    align-items: center;
	    justify-content: center;
	    height: 100%;
	  }
	</style>
</head>

<body>
	<div layout:fragment="fragment_body">
		<van-row>
			<van-form @submit="onShare">
				<van-field v-model="shareTitle" placeholder="请输入标题"
					:rules="[{ required: true}]" ></van-field>
				<van-field
				  v-model="shareText"
				  rows="3"
				  type="textarea"
				  maxlength="500"
				  placeholder="请输入内容"
				  show-word-limit 
				  :rules="[{ required: true}]"
				  ></van-field>
				<div style="margin: 16px;float: right;">
					<van-button round type="info" native-type="submit" v-if="accountInfo.accountId">发布</van-button>
					<van-button round type="info" v-if="!accountInfo.accountId" disabled>发布</van-button>
				</div>
			</van-form>
		</van-row>
		<van-divider :style="{ color: '#1989fa', borderColor: '#1989fa', padding: '0 15px', margin: '6px 0' }">分享列表</van-divider>
		<van-row>
			<van-empty v-if="shares == null || shares.length == 0" description="暂无分享记录" ></van-empty>
			<van-card
			  :desc="share.shareSubtitle == 'admin'?'管理员':share.account.accountNick"
			  :title="share.shareTitle"
			  v-for="(share, index) in shares" :key="index" @click="shareCilck(share)">
			    <template #num>
					<van-button size="mini" plain type="info"
					@click="detailCilck(share)">查看</van-button>
					<van-button size="mini" plain type="primary" 
					v-if="accountInfo.accountId && share.account.accountRole*1 == 1"  
					@click="guideCilck(share.account.accountId)">咨询</van-button>
				</template>
				<template #thumb>
					<van-image v-if="share.shareSubtitle == 'admin'" contain round width="2.5rem" height="2.5rem" src="/img/head.png" ></van-image>
					<van-image v-if="share.shareSubtitle !== 'admin'" contain round width="2.5rem" height="2.5rem" :src="share.account.accountLogo" ></van-image>
				</template>
				<template #price>
					{{share.shareTime}}
				</template>
			  </van-card>
		</van-row>
		<!--咨询-->
		<van-dialog v-model="showDialog" title="咨询专家" show-cancel-button show-confirm-button @close="showDialog = false"
			@confirm="onGuide" :before-close='beforeclose'>
			<!-- 输入框 -->
			<van-form ref="form">
				<van-field v-model="guideText" placeholder="请输入内容" :rules="[{ required: true}]" required rows="3"
					autosize type="textarea" maxlength="50" clearable></van-field>
			</van-form>
		</van-dialog>
	</div>
</body>
<script type="text/javascript" layout:fragment="fragment_script">
	let vueData = {
		activeMenu: 'share',
		shareTitle: '',
		shareText: '',
		shares: [],
		showDialog: false,
		guideReceiveId: '',
		guideText: ''
	}
	let vueComputed = {

	}
	let vueMethods = {
		onShare(){
			let that = this
			let sp = {
				shareTitle: that.shareTitle,
				shareText: that.shareText
			}
			postRequst('/user/saveShare', sp).then(function (resp) {
				if(resp.status === 0){
					that.getShares()
					that.shareTitle = ''
					that.shareText = ''
					that.$toast.success(resp.msg);
				}else{
					that.$toast(resp.msg);
				}
			})
		},
		getShares() {
			let that = this
			postRequst('/user/getShares', {}, true).then(function (resp) {
				that.shares = resp.rows
			})
		},
		shareCilck(share){
			console.log(share)
		},
		detailCilck(share){
			window.location.href='/user/shareDetail?sid=' + share.shareId
		},
		guideCilck(accountId){
			if(this.accountInfo.accountId === accountId){
				this.$toast('无法对自己咨询');
				return false
			}
			if(this.accountInfo.accountRole === 1){
				this.$toast('专家无法发起咨询');
				return false
			}
			this.showDialog = true
			this.guideReceiveId = accountId
		},
		beforeclose(action, done) {
			let that = this
			if (action === 'confirm' && !that.guideText) {
				this.showDialog = true;
				that.$toast('请填写');
				return done(false)
			} else {
				return done()
			}
		},
		onGuide(){
			let that  = this
			if(!that.guideText){
				return false
			}
			let gp = {
				receiveId: that.guideReceiveId,
				text: that.guideText
			}
			postRequst('/user/sendGuidance', gp, true).then(function (resp) {
				if(resp.status === 0){
					that.guideReceiveId = ''
					that.guideText = ''
					that.showDialog = false
					that.$toast.success(resp.msg);
				}else{
					that.$toast(resp.msg);
				}
			})
		},
	}
	function vueCreated() {
		let that = this
		this.getAccount()
		that.getShares()
	}
</script>

</html>